package com.example.nep.controller;

import com.example.nep.base.BaseController;
import com.example.nep.base.R;
import com.example.nep.dto.AdminPageDTO;
import com.example.nep.dto.AqiFeedBackAddDTO;
import com.example.nep.dto.AqiPageDTO;
import com.example.nep.dto.FeedBackEditDTO;
import com.example.nep.model.Aqi;
import com.example.nep.model.AqiFeedback;
import com.example.nep.model.Statistics;
import com.example.nep.service.IAqiFeedbackService;
import com.example.nep.service.IAqiService;
import com.example.nep.service.IStatisticsService;
import com.example.nep.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: 韩建平
 * @Description: TODO
 * @DateTime: 2024/4/10 15:46
 **/
@CrossOrigin
@RestController
@RequestMapping("aqi")
public class AqiFeedBackController extends BaseController {
    @Resource
    private IAqiService aqiService;
    @Resource
    private IAqiFeedbackService feedbackService;
    @Resource
    private IStatisticsService statisticsService;

    @GetMapping("showAll")
    public R showAll() {
        List<Aqi> list = aqiService.list();
        if (list.size() > 0) {
            return R.ok().setResult("aqis", list);
        } else {
            return R.error("未找到值");
        }
    }

    @PostMapping("insert")
    public R insert(@RequestBody AqiFeedBackAddDTO feedBackAddDTO) {
        String telId = uservo().getTelId();
        feedBackAddDTO.setTelId(telId);
        System.out.println(feedBackAddDTO.getInformation());
        int i = feedbackService.addFeedBack(feedBackAddDTO);
        if (i > 0) {
            return R.ok("添加成功!");
        } else {
            return R.error("添加失败");
        }
    }

    @GetMapping("findBackAll")
    public R findBackAll(@RequestParam Integer pageSize,
                         @RequestParam Integer lastAfId) {
        String telId = uservo().getTelId();
        List<FeedBackListVO> lists = feedbackService.findBackAll(pageSize, lastAfId, telId);
        System.out.println(lists.size());
        if (lists.size() > 0) {
            return R.ok().setResult("list", lists);
        } else {
            return R.error().setResult("list", lists);
        }
    }

    @GetMapping("gmPage")
    public R gmPage(@RequestParam Integer pageSize,
                    @RequestParam Integer lastAfId) {
        String gmCode = uservo().getGmCode();
        List<FeedBackListVO> lists = feedbackService.gmPage(pageSize, lastAfId, gmCode);
        if (lists.size() > 0) {
            return R.ok().setResult("list", lists);
        } else {
            return R.error().setResult("list", lists);
        }
    }

    @PostMapping("adminPage")
    public R adminPage(@RequestBody AdminPageDTO adminPageDTO) {
        PageVO<FeedbackShowVO> page = feedbackService.adminPage(adminPageDTO);
        return R.ok().setResult("feedbacks",page);
    }

    @PostMapping("aqiPage")
    public R adminPage(@RequestBody AqiPageDTO aqiPageDTO) {
        PageVO<AqiDetailVO> page = feedbackService.aqiPage(aqiPageDTO);
        return R.ok().setResult("aqis",page);
    }

    /**
     * 网格员查看反馈信息详情
     * @param afId
     * @return
     */
    @GetMapping("detail")
    public R feedBackDetail(@RequestParam("afId") Integer afId) {
        String gmCode = uservo().getGmCode();
        FeedBackDetailVO backDetail = feedbackService.findBackDetail(afId);
        if (backDetail != null) {
            return R.ok().setResult("feedback", backDetail);
        } else {
            return R.error();
        }
    }

    /**
     * 管理员查看反馈信息详情
     * @param afId
     * @return
     */
    @GetMapping("feedbackDetail")
    public R detail(@RequestParam("afId") Integer afId){
        FeedBackDetailVO detail = feedbackService.detail(afId);
        return R.ok().setResult("detail",detail);
    }
    /**
     * 管理员查看确认信息详情
     * @param afId
     * @return
     */
    @GetMapping("aqiDetail")
    public R aqiDetail(@RequestParam("afId") Integer afId){
        AqiDetailVO detail = feedbackService.aqiDetail(afId);
        return R.ok().setResult("detail",detail);
    }

    /**
     * 网格员更新反馈信息
     * @param feedBackEditDTO
     * @return
     */
    @PutMapping("updateInfo")
    public R updateInfo(@RequestBody FeedBackEditDTO feedBackEditDTO) {
        Statistics statistics = new Statistics();
        statistics.setId(feedBackEditDTO.getAfId());
        BeanUtils.copyProperties(feedBackEditDTO, statistics);
        int i = statisticsService.updateInfo(statistics);
        if (i > 0) {
            return R.ok();
        } else {
            return R.error();
        }
    }
    @GetMapping("setGm")
    public R setGm(@RequestParam Integer afId,@RequestParam Integer gmId){
        int i = feedbackService.setGm(afId, gmId);
        if (i>0){
            return R.ok();
        }else {
            return R.error();
        }
    }


}
